package com.frinder.base;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;

/**
 * @ClassName ByteBufUtils
 * @Description 工具类
 * @Author frinder
 * @Date 2019/4/17 9:54
 * @Verion 1.0
 */
public class ByteBufUtils {

    /**
     * @Author frinder
     * @Description str 转化为 byte buf
     * @Date 2019/4/17 9:52
     * @Param [str]
     * @Return io.netty.buffer.ByteBuf
     */
    public static ByteBuf buf(String str) {
        ByteBuf buf = Unpooled.buffer();
        buf.writeBytes(str.getBytes());
        return buf;
    }

    /**
     * @Author frinder
     * @Description str 转化为 byte buf，并添加后缀 suffix
     * @Date 2019/4/17 9:55
     * @Param [str, suffix]
     * @Return io.netty.buffer.ByteBuf
     */
    public static ByteBuf buf(String str, String suffix) {
        ByteBuf buf = Unpooled.buffer();
        buf.writeBytes(str.getBytes());
        buf.writeBytes(suffix.getBytes());
        return buf;
    }

    /**
     * @Author frinder
     * @Description byte buf 转化为 str
     * @Date 2019/4/17 10:11
     * @Param [buf]
     * @Return java.lang.String
     */
    public static String str(ByteBuf buf) {
        return new String(ByteBufUtil.getBytes(buf));
    }

}
